package com.ai.service.cms.user.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.ai.common.exception.BusinessException;
import com.ai.manager.edu.cms.user.RoleMenuManager;
import com.ai.model.edu.cms.backoperator.RoleMenu;
import com.ai.model.edu.cms.backoperator.dto.RoleMenuDTO;
import com.ai.model.edu.cms.backoperator.vo.RoleMenuVO;
import com.ai.service.cms.user.RoleMenuService;

@Service("roleMenuService")
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    private RoleMenuManager roleMenuManager;

    /**
     * 
     * Method Name : selectByRoleId<br>
     * 
     * Description : 更具roleId查询其对应的menu<br>
     * 
     * @param roleId
     * @return
     * @since
     *
     */
    public List<RoleMenu> selectByRoleId(Long roleId) {
        return roleMenuManager.selectByRoleId(roleId);
    }

    /**
     * 
     * Method Name : updateRoleMenu<br>
     * 
     * Description : 更新觉得roleId的菜单权限<br>
     * 
     * @param roleMenuDTO
     * @param roleId
     * @param backOperatorId
     * @return
     * @since
     *
     */
    public String updateRoleMenu(RoleMenuDTO roleMenuDTO, Long operatorId) {

        if (roleMenuDTO == null || roleMenuDTO.getRoleId() == null) {
            BusinessException.throwMessage("传入参数角色roleId为空"); // 抛出异常
        }

        if (roleMenuDTO == null || roleMenuDTO.getRoleId() == null) {
            BusinessException.throwMessage("传入参数角色roleId为空"); // 抛出异常
        }

        // 当前角色已经已有的menu
        List<RoleMenu> roleMenuLst = roleMenuManager.selectByRoleId(roleMenuDTO.getRoleId());
        // 页面传入的menu
        List<Long> menuIdLst = roleMenuDTO.getMenuIdLst();

        if (CollectionUtils.isEmpty(menuIdLst)) { // 传入的菜单ID为空
            for (RoleMenu roleMenu : roleMenuLst) {
                // 删除
                roleMenuManager.deleteByPrimaryKey(roleMenu.getId());
            }
            return "更新成功";
        }

        // 公共的menuId
        List<Long> menuIdCommonLst = new ArrayList<Long>();
        for (RoleMenu roleMenu : roleMenuLst) {
            Long menuIdOld = roleMenu.getMenuId();
            if (menuIdOld != null && menuIdLst.contains(menuIdOld)) {
                menuIdCommonLst.add(menuIdOld);
            }
        }

        // 处理旧记录
        for (RoleMenu roleMenu : roleMenuLst) {
            if (CollectionUtils.isNotEmpty(menuIdCommonLst) && !menuIdCommonLst.contains(roleMenu.getMenuId())) {
                // 删除
                roleMenuManager.deleteByPrimaryKey(roleMenu.getId());
            }
        }

        // 处理页面记录
        for (Long menuId : menuIdLst) {
            if (CollectionUtils.isNotEmpty(menuIdCommonLst) && menuIdCommonLst.contains(menuId)) {
                // 更新(此表为关系表，不需要更新)
                // RoleMenu roleMenu = new RoleMenu();
                // BeanUtils.copyProperties(roleMenuMap.get(menuId), roleMenu);
                // roleMenuManager.updateByPrimaryKey(roleMenu);
            } else {
                // 插入
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleMenuDTO.getRoleId());
                roleMenu.setMenuId(menuId);
                roleMenu.setCreateBy(operatorId);
                roleMenuManager.insert(roleMenu);
            }
        }

        return "更新成功";
    }

    /**
     * Description ： 根据角色ids 查询角色对应的菜单<br>
     * 
     * yangwb
     * 
     * @param roleIds
     * @return
     * @since
     *
     */
    public List<RoleMenuVO> findRoleMunuVOsByRoleIds(List<Long> roleIds) {
        return roleMenuManager.findRoleMunuVOsByRoleIds(roleIds);
    }

}